#include "bsp_key.h"
#include "systick.h"

static void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
	NVIC_InitStructure.NVIC_IRQChannel = KEY1_INT_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = KEY2_INT_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = KEY_Micro_INT_EXTI_IRQ;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void KEY_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    
    RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Pin=KEY1_GPIO_PIN|KEY2_GPIO_PIN;/*选择GPIO引脚*/
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;/*推挽输出*/
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;/*引脚频率50mhz*/
    GPIO_Init(GPIOA,&GPIO_InitStructure);/*调用库函数，初始化GPIO*/
	
	RCC_APB2PeriphClockCmd(KEY_Micro_GPIO_CLK,ENABLE);
    GPIO_InitStructure.GPIO_Pin=KEY_Micro_GPIO_PIN;/*选择GPIO引脚*/
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;/*推挽输出*/
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;/*引脚频率50mhz*/
    GPIO_Init(KEY_Micro_GPIO_PORT,&GPIO_InitStructure);/*调用库函数，初始化GPIO*/
	
	
}
void KEY_INT_GPIO_Config(void)
{
		EXTI_InitTypeDef EXTI_Initstructure;
		RCC_APB2PeriphClockCmd(GPIO_AFIO_CLK, ENABLE);	  
		NVIC_Configuration();
		GPIO_EXTILineConfig(KEY1_INT_EXTI_PORTSOURCE,KEY1_INT_EXTI_PINSOURCE);
		EXTI_Initstructure.EXTI_Line=KEY1_INT_EXTI_LINE;   
		EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt; 
		EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling; 
		EXTI_Initstructure.EXTI_LineCmd=ENABLE;     
		EXTI_Init(&EXTI_Initstructure);  
	
		GPIO_EXTILineConfig(KEY2_INT_EXTI_PORTSOURCE,KEY2_INT_EXTI_PINSOURCE);	
		EXTI_Initstructure.EXTI_Line=KEY2_INT_EXTI_LINE;  	
		EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt; 
		EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling; 
		EXTI_Initstructure.EXTI_LineCmd=ENABLE;      
		EXTI_Init(&EXTI_Initstructure);    
	
		GPIO_EXTILineConfig(KEY_Micro_INT_EXTI_PORTSOURCE,KEY_Micro_INT_EXTI_PINSOURCE);	
		EXTI_Initstructure.EXTI_Line=KEY_Micro_INT_EXTI_LINE;  	
		EXTI_Initstructure.EXTI_Mode=EXTI_Mode_Interrupt; 
		EXTI_Initstructure.EXTI_Trigger=EXTI_Trigger_Falling; 
		EXTI_Initstructure.EXTI_LineCmd=ENABLE;      
		EXTI_Init(&EXTI_Initstructure);          	
}

uint8_t KEY1_SCAN(void)
{
	static uint8_t key1_old=1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
	{
		key1_old=key1_now;return 1;
	}
	else 
	{
		key1_old=key1_now;return 0;
	}
}
uint8_t KEY2_SCAN(void)
{
	static uint8_t key2_old=1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
	{
		key2_old=key2_now;return 1;
	}
	else 
	{
		key2_old=key2_now;return 0;
	}
}
uint8_t KEY_Micro_SCAN(void)
{
	static uint8_t key_micro_old=1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 && key_micro_now==0)
	{
		key_micro_old=key_micro_now;return 1;
	}
	else 
	{
		key_micro_old=key_micro_now;return 0;
	}
}

uint8_t key1_pressed=0;
void KEY1_SCAN_FLAG(void)
{
	static uint8_t key1_old = 1;
	uint8_t key1_now;
	key1_now=GETKEY1();
	if(key1_old==1 &&key1_now==0)
		key1_pressed=1;
	key1_old=key1_now;
}

uint8_t key2_pressed=0;
void KEY2_SCAN_FLAG(void)
{
	static uint8_t key2_old = 1;
	uint8_t key2_now;
	key2_now=GETKEY2();
	if(key2_old==1 &&key2_now==0)
		key2_pressed=1;
	key2_old=key2_now;
}

uint8_t key_micro_pressed=0;
void KEY_Micro_SCAN_FLAG(void)
{	
	static uint8_t key_micro_old = 1;
	uint8_t key_micro_now;
	key_micro_now=GETKEY_Micro();
	if(key_micro_old==1 &&key_micro_now==0)
		key_micro_pressed=1;
	key_micro_old=key_micro_now;
}

uint8_t num = 0;                  // 变量初始化
uint8_t led4_flash = 0;
uint8_t micro_pressed = 0;
uint8_t led_cycle = 1;         // 默认LED循环	
